'''
* This is the projet for Brtc LlmOps Platform
* @Author Leon-liao <liaosiliang@alltman.com>
* @Description //TODO 
* @File: 7_study_llm_retry.py
* @Time: 2025/7/27
* @All Rights Reserve By Brtc
'''
from langchain_core.runnables import RunnableLambda

"""7、Runnable 组件重试 / 回退机制降低程序错误率  a、Runnable 重试机制"""
counter = -1

def func(x):
    global counter
    counter += 1
    print(f"counter: {counter}")
    if counter == 5:
        return 2
    else:
        2/0

chain = RunnableLambda(func).with_retry(stop_after_attempt=6)

resp = chain.invoke(2)
print(resp)